﻿@page "/Archive/License/Index"
@page "/Archive/License/Index/{Token}"
@using iTeamyou.LTS.UI.Models;
@using iTeamyou.LTS.UI.Shared.Components;

<h5 style="margin-top: 1.5rem;">管理证照</h5>
<Tab>
    <TabItem Text="商品证照">
        <div style="margin-top: 2rem;">
            <div>
                <Button Text="查看" OnClick="@EditProductLicenseBtn" IsDisabled="@(SelectedProductLicenses.Count != 1)" />
                <Button IsAsync="true" Text="导出" OnClick="@ExportProductLicenses" IsDisabled="IsUnauth" />
                <div style="float: right">
                    <label style="font-size:xx-large; color: orange">@ProductLicenseCount</label><label> 个证照素材</label>
                </div>
            </div>
        </div>
        <Table style="margin-top: 1rem;" TItem="Core.Models.ProductLicense"
               IsPagination="true" PageItemsSource="@PageItemsSource" IsStriped="true" ShowLineNo="true"
               CustomerSearchModel="@ProductSearchModel" SearchMode="SearchMode.Top" ShowSearchText="true" ShowSearch="true"
               OnDoubleClickRowCallback="@EditProductLicense"
               SelectedRows="@SelectedProductLicenses" SelectedRowsChanged="@ProductLicenseSelectChanged" IsMultipleSelect="true"
               ShowSkeleton="true" ShowResetButton="true" ShowEmpty="true"
               OnQueryAsync="@OnProductLicenseQueryAsync" IsAutoRefresh="@IsAutoRefresh" AutoRefreshInterval="2000">
            <TableColumns>
                <TableColumn @bind-Field="@context.LId" Width="80" />
                <TableColumn @bind-Field="@context.Name" Width="100" />
                <TableColumn @bind-Field="@context.Type" Width="100">
                    <Template Context="value">
                        <Tag Color="Color.Primary">@GetLicenseTypeName(value.Value, "product")</Tag>
                    </Template>
                </TableColumn>
                <TableColumn @bind-Field="@context.ProductId" Text="商品编码" Width="60">
                    <Template Context="value">
                        @if (value.Value != null)
                        {
                            <div>@ProductService.Get(value.Value)?.PId</div>
                        }
                    </Template>
                </TableColumn>
                <TableColumn @bind-Field="@context.ProductName" Width="100" />
                <TableColumn @bind-Field="@context.SupplierName" Width="100" />
                <TableColumn @bind-Field="@context.RemainingDays" Width="80" Sortable="true" Text="剩余有效时间">
                    <Template Context="value">
                        @if (value.Row.ValidToDate == null)
                        {
                            <Tag Color="Color.Primary">长期有效</Tag>
                        }
                        else if (((DateTime)value.Row.ValidToDate - DateTime.Now).Days <= 0)
                        {
                            <Tag Color="Color.Danger">已过期</Tag>
                        }
                        else
                        {
                            <Tag Color="Color.Primary">@(((DateTime)value.Row.ValidToDate - DateTime.Now).Days+" 天")</Tag>
                        }
                    </Template>
                </TableColumn>
                <TableColumn @bind-Field="@context.ValidToDate" Width="100" />
                @*<TableColumn @bind-Field="@context.ProductLicenses" Width="80" Text="证照数">
            <Template Context="value">
                @if (value.Value == null || value.Value.Count == 0)
                {
                    <Tag Color="Color.Dark">共 0 个证照</Tag>
                }
                else
                {(DateTime)context.ValidToDate - (DateTime)context.ValidFromDate).Days
                    <Tag Color="Color.Primary">共 @value.Value.Count 个证照</Tag>
                }
            </Template>
        </TableColumn>*@
            </TableColumns>
            <CustomerSearchTemplate>
                @if (context is ProductSearchModel model)
                {
                    <ProductLicenseSearch @bind-Value="@model" />
                }
            </CustomerSearchTemplate>
        </Table>
    </TabItem>
    <TabItem Text="批次证照">
        <div style="margin-top: 2rem;">
            <Button Text="查看" OnClick="@EditBatchLicenseBtn" IsDisabled="@(SelectedBatchLicenses.Count != 1)" />
            <Button IsAsync="true" Text="导出" OnClick="@ExportBatchLicenses" IsDisabled="IsUnauth" />
            <div style="float: right">
                <label style="font-size:xx-large; color: orange">@BatchLicenseCount</label><label> 个证照素材</label>
            </div>
        </div>
        <Table style="margin-top: 1rem;" TItem="Core.Models.BatchLicense"
               IsPagination="true" PageItemsSource="@PageItemsSource" IsStriped="true" ShowLineNo="true"
               CustomerSearchModel="@BatchSearchModel" SearchMode="SearchMode.Top" ShowSearchText="true" ShowSearch="true"
               OnDoubleClickRowCallback="@EditBatchLicense"
               SelectedRows="@SelectedBatchLicenses" SelectedRowsChanged="@BatchLicenseSelectChanged" IsMultipleSelect="true"
               ShowSkeleton="true" ShowResetButton="true" ShowEmpty="true"
               OnQueryAsync="@OnBatchLicenseQueryAsync" IsAutoRefresh="@IsAutoRefresh" AutoRefreshInterval="2000">
            <TableColumns>
                <TableColumn @bind-Field="@context.LId" Width="80" />
                <TableColumn @bind-Field="@context.Name" Width="100" />
                <TableColumn @bind-Field="@context.Type" Width="100">
                    <Template Context="value">
                        <Tag Color="Color.Primary">@GetLicenseTypeName(value.Value, "batch")</Tag>
                    </Template>
                </TableColumn>
                <TableColumn @bind-Field="@context.Number" Width="100" />
                <TableColumn @bind-Field="@context.EntryDatetime" Width="100" />
                <TableColumn @bind-Field="@context.RemainingDays" Width="80" Sortable="true" Text="剩余有效时间">
                    <Template Context="value">
                        @if (value.Row.ValidToDate == null)
                        {
                            <Tag Color="Color.Primary">长期有效</Tag>
                        }
                        else if (((DateTime)value.Row.ValidToDate - DateTime.Now).Days <= 0)
                        {
                            <Tag Color="Color.Danger">已过期</Tag>
                        }
                        else
                        {
                            <Tag Color="Color.Primary">@(((DateTime)value.Row.ValidToDate - DateTime.Now).Days+" 天")</Tag>
                        }
                    </Template>
                </TableColumn>
                <TableColumn @bind-Field="@context.ValidToDate" Width="100" />
            </TableColumns>
            <CustomerSearchTemplate>
                @if (context is BatchSearchModel model)
                {
                    <BatchLicenseSearch @bind-Value="@model" />
                }
            </CustomerSearchTemplate>
        </Table>
    </TabItem>
    <TabItem Text="供应商证照">
        <div style="margin-top: 2rem;">
            <Button Text="查看" OnClick="@EditSupplierLicenseBtn" IsDisabled="@(SelectedSupplierLicenses.Count != 1)" />
            <Button IsAsync="true" Text="导出" OnClick="@ExportSupplierLicenses" IsDisabled="IsUnauth" />
            <div style="float: right">
                <label style="font-size:xx-large; color: orange">@SupplierLicenseCount</label><label> 个证照素材</label>
            </div>
        </div>
        <Table style="margin-top: 1rem;" TItem="Core.Models.SupplierLicense"
               IsPagination="true" PageItemsSource="@PageItemsSource" IsStriped="true" ShowLineNo="true"
               CustomerSearchModel="@SupplierSearchModel" SearchMode="SearchMode.Top" ShowSearchText="true" ShowSearch="true"
               OnDoubleClickRowCallback="@EditSupplierLicense"
               SelectedRows="@SelectedSupplierLicenses" SelectedRowsChanged="@SupplierLicenseSelectChanged" IsMultipleSelect="true"
               ShowSkeleton="true" ShowResetButton="true" ShowEmpty="true"
               OnQueryAsync="@OnSupplierLicenseQueryAsync" IsAutoRefresh="@IsAutoRefresh" AutoRefreshInterval="2000">
            <TableColumns>
                <TableColumn @bind-Field="@context.LId" Width="80" />
                <TableColumn @bind-Field="@context.Name" Width="100" />
                <TableColumn @bind-Field="@context.Type" Width="100">
                    <Template Context="value">
                        <Tag Color="Color.Primary">@GetLicenseTypeName(value.Value, "supplier")</Tag>
                    </Template>
                </TableColumn>
                <TableColumn @bind-Field="@context.SupplierId" Text="供应商编码" Width="100">
                    <Template Context="value">
                        @if (value.Value != null)
                        {
                            <div>@SupplierService.Get(value.Value)?.SId</div>
                        }
                    </Template>
                </TableColumn>
                <TableColumn @bind-Field="@context.SupplierName" Width="100" />
                <TableColumn @bind-Field="@context.RemainingDays" Width="80" Sortable="true" Text="剩余有效时间">
                    <Template Context="value">
                        @if (value.Row.ValidToDate == null)
                        {
                            <Tag Color="Color.Primary">长期有效</Tag>
                        }
                        else if (((DateTime)value.Row.ValidToDate - DateTime.Now).Days <= 0)
                        {
                            <Tag Color="Color.Danger">已过期</Tag>
                        }
                        else
                        {
                            <Tag Color="Color.Primary">@(((DateTime)value.Row.ValidToDate - DateTime.Now).Days+" 天")</Tag>
                        }
                    </Template>
                </TableColumn>
                <TableColumn @bind-Field="@context.ValidToDate" Width="100" />
            </TableColumns>
            <CustomerSearchTemplate>
                @if (context is SupplierSearchModel model)
                {
                    <SupplierLicenseSearch @bind-Value="@model" />
                }
            </CustomerSearchTemplate>
        </Table>
    </TabItem>
    <TabItem Text="生产商证照">
        <div style="margin-top: 2rem;">
            <Button Text="查看" OnClick="@EditManufacturerLicenseBtn" IsDisabled="@(SelectedManufacturerLicenses.Count != 1)" />
            <Button IsAsync="true" Text="导出" OnClick="@ExportManufacturerLicenses" IsDisabled="IsUnauth" />
            <div style="float: right">
                <label style="font-size:xx-large; color: orange">@ManufacturerLicenseCount</label><label> 个证照素材</label>
            </div>
        </div>
        <Table style="margin-top: 1rem;" TItem="Core.Models.ManufacturerLicense"
               IsPagination="true" PageItemsSource="@PageItemsSource" IsStriped="true" ShowLineNo="true"
               CustomerSearchModel="@ManufacturerSearchModel" SearchMode="SearchMode.Top" ShowSearchText="true" ShowSearch="true"
               OnDoubleClickRowCallback="@EditManufacturerLicense"
               SelectedRows="@SelectedManufacturerLicenses" SelectedRowsChanged="@ManufacturerLicenseSelectChanged" IsMultipleSelect="true"
               ShowSkeleton="true" ShowResetButton="true" ShowEmpty="true"
               OnQueryAsync="@OnManufacturerLicenseQueryAsync" IsAutoRefresh="@IsAutoRefresh" AutoRefreshInterval="2000">
            <TableColumns>
                <TableColumn @bind-Field="@context.LId" Width="80" />
                <TableColumn @bind-Field="@context.Name" Width="100" />
                <TableColumn @bind-Field="@context.Type" Width="100">
                    <Template Context="value">
                        <Tag Color="Color.Primary">@GetLicenseTypeName(value.Value, "manufacturer")</Tag>
                    </Template>
                </TableColumn>
                <TableColumn @bind-Field="@context.ManufacturerId" Text="生产商编号" Width="100">
                    <Template Context="value">
                        @if (value.Value != null)
                        {
                            <div>@ManufacturerService.Get(value.Value)?.MId</div>
                        }
                    </Template>
                </TableColumn>
                <TableColumn @bind-Field="@context.ManufacturerName" Width="100" />
                <TableColumn @bind-Field="@context.RemainingDays" Width="80" Sortable="true" Text="剩余有效时间">
                    <Template Context="value">
                        @if (value.Row.ValidToDate == null)
                        {
                            <Tag Color="Color.Primary">长期有效</Tag>
                        }
                        else if (((DateTime)value.Row.ValidToDate - DateTime.Now).Days <= 0)
                        {
                            <Tag Color="Color.Danger">已过期</Tag>
                        }
                        else
                        {
                            <Tag Color="Color.Primary">@(((DateTime)value.Row.ValidToDate - DateTime.Now).Days+" 天")</Tag>
                        }
                    </Template>
                </TableColumn>
                <TableColumn @bind-Field="@context.ValidToDate" Width="100" />
            </TableColumns>
            <CustomerSearchTemplate>
                @if (context is ManufacturerSearchModel model)
                {
                    <ManufacturerLicenseSearch @bind-Value="@model" />
                }
            </CustomerSearchTemplate>
        </Table>
    </TabItem>
</Tab>